# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Contributor: Daniel Micay <danielmicay@gmail.com>
# Contributor: Mladen Pejakovic <pejakm@gmail.com>

pkgbase=libxkbcommon
pkgname=(
  libxkbcommon
  libxkbcommon-x11
  libxkbcommon-doc
)
pkgver=1.5.0
pkgrel=1
pkgdesc='Keymap handling library for toolkits and window systems'
url='https://xkbcommon.org/'
arch=(x86_64)
license=(custom)
makedepends=(
  doxygen
  git
  libxcb 
  libxml2
  meson
  wayland
  wayland-protocols
  xkeyboard-config
  xorgproto
)
checkdepends=(
  libgl
  xorg-server-xvfb
)
options=(
  debug
)
_commit=cecaa01df18925eb66c6a81f6e4b7167421d45f2  # tags/xkbcommon-1.5.0^0
source=("git+https://github.com/xkbcommon/libxkbcommon#commit=$_commit")
sha256sums=('SKIP')

pkgver() {
  cd libxkbcommon
  git describe --tags | sed 's/^xkbcommon-//;s/[^-]*-g/r&/;s/-/+/g'
}

prepare() {
  cd libxkbcommon

  # Fix xkbcli interactive-wayland
  git cherry-pick -n 320f56d2a4a31026e5c8d3b60303af98bf885e1b
}

build() {
  arch-meson libxkbcommon build
  meson compile -C build
}

check() {
  xvfb-run -s '-nolisten local' meson test -C build --print-errorlogs
}

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

package_libxkbcommon() {
  depends=(
    libxml2
    xkeyboard-config
  )
  optdepends=(
    'libxkbcommon-x11: xkbcli interactive-x11'
    'wayland: xkbcli interactive-wayland'
  )
  provides=(
    libxkbcommon.so
    libxkbregistry.so
  )

  meson install -C build --destdir "$pkgdir"

  _pick x11 "$pkgdir"/usr/lib/*x11*
  _pick x11 "$pkgdir"/usr/lib/pkgconfig/*x11*
  _pick x11 "$pkgdir"/usr/lib/xkbcommon/*x11*
  _pick x11 "$pkgdir"/usr/include/xkbcommon/*x11*
  _pick x11 "$pkgdir"/usr/share/man/man1/*x11*

  _pick doc "$pkgdir"/usr/share/doc

  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 libxkbcommon/LICENSE
}

package_libxkbcommon-x11() {
  pkgdesc='Keyboard handling library using XKB data for X11 XCB clients'
  depends=(
    libxcb
    libxkbcommon
    libxkbcommon.so
  )
  provides=(
    libxkbcommon-x11.so
  )

  mv x11/* "$pkgdir"/

  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 libxkbcommon/LICENSE
}

package_libxkbcommon-doc() {
  pkgdesc='API documentation for libxkbcommon'

  mv doc/* "$pkgdir"/

  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 libxkbcommon/LICENSE
}

# vim:set sw=2 sts=-1 et:
